package asionreachel.cn.smarthomedemo.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;

/**
 * Created by Simon(lyd) on 2016/9/27:30.
 * 描述：App中所有Activity的基类
 */

public class BaseActivity extends Activity {
    private long m_lLastBackKeyTime = 0;
    public boolean m_bRun = false;
    public Activity mActivity = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        /** 使用沉淀式菜单 */
        onSetMenuStyle();

        ASManager.GetInstance().AddActivity( this );
//        BugoutConfig config = new BugoutConfig.Builder(this.getApplicationContext())
//                .withAppKey("fa270077c6ec421eebe228d655969a87")     // 您的应用的项目 Key,如果已经在 Manifest 中配置则此处可略
//                .withAppChannel("BathHeader")     // 发布应用的渠道,如果已经在 Manifest 中配置则此处可略
//                .withUserInfo("")    // 用户信息-崩溃分析根据用户记录崩溃信息
//                .withDebugModel(true)    // 输出更多SDK的debug信息
//                .withErrorActivity(true)    // 发生崩溃时采集Activity信息
//                .withCollectNDKCrash(true) //  收集NDK崩溃信息
//                .withOpenCrash(true)    // 收集崩溃信息开关
//                .withOpenEx(true)     // 是否收集异常信息
//                .withReportOnlyWifi(true)    // 仅在 WiFi 下上报崩溃信息
//                .withReportOnBack(true)    // 当APP在后台运行时,是否采集信息
//                .withQAMaster(true)    // 是否收集摇一摇反馈
//                .withCloseOption(false)   // 是否在摇一摇菜单展示‘关闭摇一摇选项’
//                .withLogCat(true)  // 是否系统操作信息
//                .build();
//        Bugout.init(config);
    }

    @Override
    protected void onResume() {
        m_bRun = true;
        super.onResume();
        /** 设置竖屏 */
        onSetWindowsPortrait();
    }

    @Override
    protected void onDestroy() {
        ASManager.GetInstance().DelActivity( this );
        super.onDestroy();
    }

    @Override
    protected void onPause(){
        m_bRun = false;
        super.onPause();
    }

    /**
     * 点击返回按钮回调
     */
    public void onBackKey()
    {
        if (System.currentTimeMillis() - m_lLastBackKeyTime > 1500)
        {
            Common.showToast("再按一次返回键退出");
            m_lLastBackKeyTime = System.currentTimeMillis();
        }
        else
        {
            //退出程序
            ASManager.GetInstance().FinishAllActivity();
        }
    }

    /**
     * 隐藏标题栏
     * 因为AppCompatActivity会自带标题栏
     */
    public void onHideSystemBar() {
//        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 设置App固定为竖屏显示
     */
    public void onSetWindowsPortrait() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {  //判断当前是否为竖屏显示
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 设置菜单栏样式
     * 使用沉淀式菜单
     */
    public void onSetMenuStyle() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(android.R.color.transparent);
    }

    public void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            onBackKey();
        }
        return true;
        //return super.onKeyDown(keyCode, event);
    }
}
